package com.ruoyi.combat.service.useralignment.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.combat.domain.useralignment.*;
import com.ruoyi.combat.domain.userlocation.CombatUser;
import com.ruoyi.combat.mapper.useralignment.*;
import com.ruoyi.combat.util.weibo.WeiboApiUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.service.useralignment.ICombatUserWbService;

/**
 * 课题五微博用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class CombatUserWbServiceImpl implements ICombatUserWbService 
{
    @Autowired
    private CombatUserWbMapper combatUserWbMapper;
    @Autowired
    private CombatBlogWbMapper combatBlogWbMapper;
    @Autowired
    private CombatRelationWbMapper combatRelationWbMapper;
    @Autowired
    private CombatAlignmentWarnMapper combatAlignmentWarnMapper;
    @Autowired
    private CombatAlignmentTaskMapper combatAlignmentTaskMapper;


    @Autowired
    private WeiboApiUtil weiboApiUtil;

    /**
     * 查询课题五微博用户信息
     * 
     * @param id 课题五微博用户信息主键
     * @return 课题五微博用户信息
     */
    @Override
    public CombatUserWb selectCombatUserWbById(Long id)
    {
        return combatUserWbMapper.selectCombatUserWbById(id);
    }

    /**
     * 查询课题五微博用户信息列表
     * 
     * @param combatUserWb 课题五微博用户信息
     * @return 课题五微博用户信息
     */
    @Override
    public List<CombatUserWb> selectCombatUserWbList(CombatUserWb combatUserWb)
    {
        return combatUserWbMapper.selectCombatUserWbList(combatUserWb);
    }

    /**
     * 新增课题五微博用户信息
     * 
     * @param combatUserWb 课题五微博用户信息
     * @return 结果
     */
    @Override
    public int insertCombatUserWb(CombatUserWb combatUserWb)
    {
        return combatUserWbMapper.insertCombatUserWb(combatUserWb);
    }

    /**
     * 修改课题五微博用户信息
     * 
     * @param combatUserWb 课题五微博用户信息
     * @return 结果
     */
    @Override
    public int updateCombatUserWb(CombatUserWb combatUserWb)
    {
        return combatUserWbMapper.updateCombatUserWb(combatUserWb);
    }

    /**
     * 批量删除课题五微博用户信息
     * 
     * @param ids 需要删除的课题五微博用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatUserWbByIds(Long[] ids)
    {

        for (Long id : ids){
            CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbById(id);
            String userId = combatUserWb.getUserId();

            CombatBlogWb  combatBlogWb = new CombatBlogWb();
            combatBlogWb.setUserId(userId);
            List<CombatBlogWb> combatBlogWbs = combatBlogWbMapper.selectCombatBlogWbList(combatBlogWb);
            for (CombatBlogWb blog : combatBlogWbs) {
                combatBlogWbMapper.deleteCombatBlogWbById(blog.getId());
            }

            List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectRelationByUserId(userId);
            for (CombatRelationWb relation : combatRelationWbs){
                combatRelationWbMapper.deleteCombatRelationWbById(relation.getId());
            }

            CombatAlignmentWarn warn = new CombatAlignmentWarn();
            warn.setUserFromId(userId);
            List<CombatAlignmentWarn> combatAlignmentWarns = combatAlignmentWarnMapper.selectCombatAlignmentWarnList(warn);
            for (CombatAlignmentWarn war: combatAlignmentWarns){
                combatAlignmentWarnMapper.deleteCombatAlignmentWarnById(war.getId());
            }

            CombatAlignmentTask combatAlignmentTask = new CombatAlignmentTask();
            combatAlignmentTask.setFromUserId(userId);
            List<CombatAlignmentTask> combatAlignmentTasks = combatAlignmentTaskMapper.selectCombatAlignmentTaskList(combatAlignmentTask);
            for (CombatAlignmentTask task: combatAlignmentTasks){
                combatAlignmentTaskMapper.deleteCombatAlignmentTaskById(task.getId());
            }

        }



        return combatUserWbMapper.deleteCombatUserWbByIds(ids);
    }

    /**
     * 删除课题五微博用户信息信息
     * 
     * @param id 课题五微博用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatUserWbById(Long id)
    {
        return combatUserWbMapper.deleteCombatUserWbById(id);
    }

    @Override
    public CombatUserWb selectCombatUserWbByUserId(String userId) {
        return combatUserWbMapper.selectCombatUserWbByUserId(userId);
    }

    @Override
    public int crawLData(List<String> ids) {
        List<String> list = new ArrayList<>();
        List<Map<String, String>> listMap = new ArrayList<>();
        for (String id : ids){
            CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(id);
            if(combatUserWb==null){
                combatUserWb = new CombatUserWb();
                combatUserWb.setUserId(id);
                combatUserWbMapper.insertCombatUserWb(combatUserWb);
            }
            list.add(id);
            Map<String, String> data = new HashMap<>();
            data.put("type", "follow");
            data.put("cookie", "");
            data.put("user_id", id);
            listMap.add(data);
        }
        String latestWeibo = weiboApiUtil.getLatestWeiboOneDay(list);
        return ids.size();
    }

    @Override
    public void crawlWeiboByUserId(String id) {
        CombatUserWb user = combatUserWbMapper.selectCombatUserWbByUserId(id);
        String userId = user.getUserId();
        String token = user.getToken();
//        获取最新微博
        List<String> list = new ArrayList<>();
        list.add(userId);
        String latestWeibo = weiboApiUtil.getLatestWeibo(list);
        if (StringUtils.isNotEmpty(token)) {
            List<Map<String, String>> listMap = new ArrayList<>();
            Map<String, String> data = new HashMap<>();
            data.put("type", "follow");
            data.put("cookie", token);
            data.put("user_id", userId);
            listMap.add(data);
            String friends = weiboApiUtil.getFriends(listMap);
        }
    }
}
